package cn.bgs.controller;

import cn.bgs.exception.PhoenixException;
import cn.bgs.pojo.*;
import cn.bgs.service.TeacherService;
import cn.bgs.utils.MyContent;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ProjectName: beisheng
 * @Package: cn.bgs.controller
 * @ClassName: TeacherController
 * @Author: Administrator
 * @Description:
 * @Date: 2021/7/21 0021 20:39
 * @Version: 1.0
 */
@Controller
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @RequestMapping("/teacherJsp")
    public String teacherJsp(){
        return "teacher";
    }
    @RequestMapping("/findTeacher")
    @ResponseBody
    public Map<String,Object> findTeacher(Model m,
                                          @RequestParam(required=false,defaultValue="1") int page,
                                          @RequestParam(required=false,defaultValue="7") int limit,
                                          String gradeName, String teacherName, String lectureName
                                          ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Grade> teacher  = teacherService.findTeacher(gradeName,teacherName,lectureName);// 这是我们的sql
        List<Classroom> classrooms = teacherService.findclassroom();
        m.addAttribute("tea",teacher);
        m.addAttribute("class",classrooms);
        // 使用pageInfo包装查询
        PageInfo<Grade> rolePageInfo = new PageInfo<>(teacher);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        return map;
    }
    @RequestMapping("/insertTeacher")
    @ResponseBody
    public Integer insertTeacher(@RequestBody String istDate){

        JSONObject jsonObject=JSONObject.parseObject(istDate);
        Grade grade = new Grade();
        grade.setGradeName(jsonObject.getString("gradeName"));
        grade.setClassTeacherId(jsonObject.getString("classTeacherId"));
        grade.setClassTeacherName(jsonObject.getString("classTeacherName"));
        grade.setLecturerId(jsonObject.getString("lecturerId"));
        grade.setLecturerName(jsonObject.getString("lecturerName"));
        grade.setStudentsNumber(jsonObject.getInteger("studentsNumber"));
        grade.setClassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        grade.setClassRemark(jsonObject.getString("classRemark"));
        grade.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();//获取时间
        grade.setCreatedate(now.getTime());
        grade.setUpdatedate(now.getTime());//当前时间
        teacherService.insertoldClassroomId(jsonObject.getString("oldclassroomClassroomId"));
        teacherService.insertclassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        return teacherService.insertTeacher(grade);
    }
    @RequestMapping("/gradeFind/{pkId}")
    public String gradeFind(@PathVariable Integer pkId,Model m){

        Grade grade = teacherService.gradeFind(pkId);
        m.addAttribute("grade",grade);
        return "/gradeStudentList";
    }
    @RequestMapping("/grade/{pkId}")
    public String grade(@PathVariable Integer pkId,Model m){

        Grade grade = teacherService.gradeFind(pkId);
        m.addAttribute("grade",grade);
        return "/gradeStudent";
    }
    @RequestMapping("/gradeFindSel/{gradeName}")
    public String gradeFindSel(@PathVariable String gradeName,Model m){

        Grade grade = teacherService.gradeFindSel(gradeName);
        m.addAttribute("grade",grade);
        return "/gradeStudent";
    }
    @RequestMapping("/deleteTeacherList")
    @ResponseBody
    public Integer deleteTeacherList(@RequestBody String jsonStr){
        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
        JSONArray ids=jsonObject.getJSONArray("pkIds");
        Integer[] pkIds = new Integer[ids.size()];
        for(int i=0;i<ids.size();i++){
            Object id = ids.get(i);
            pkIds[i]=Integer.parseInt(id.toString());
        }
        List<Integer> list = Arrays.asList(pkIds);
        return teacherService.deleteTeacherList(list);
    }
    @RequestMapping("/classroomId")
    @ResponseBody
    public List<Classroom> classroomId(){
        return teacherService.classroomId();
    }
    @RequestMapping("/teacherDelete")
    @ResponseBody
    public Integer teacherDelete(@RequestBody String data){
        JSONObject jsonObject= JSONObject.parseObject(data);
        Integer pkId=Integer.parseInt(jsonObject.getString("pkId"));
        return teacherService.teacherDelete(pkId);
    }
    @RequestMapping("/updateTeacher")
    @ResponseBody
    public Integer updateTeacher(@RequestBody String updDate){
        JSONObject jsonObject=JSONObject.parseObject(updDate);
        Grade grade = new Grade();
        grade.setPkId(jsonObject.getInteger("pkId"));
        grade.setGradeName(jsonObject.getString("gradeName"));
        grade.setClassTeacherId(jsonObject.getString("classTeacherId"));
        grade.setClassTeacherName(jsonObject.getString("classTeacherName"));
        grade.setLecturerId(jsonObject.getString("lecturerId"));
        grade.setLecturerName(jsonObject.getString("lecturerName"));
        grade.setStudentsNumber(jsonObject.getInteger("studentsNumber"));
        grade.setClassRemark(jsonObject.getString("classRemark"));
        grade.setClassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        teacherService.insertoldClassroomId(jsonObject.getString("oldclassroomClassroomId"));
        teacherService.insertclassroomClassroomId(jsonObject.getString("classroomClassroomId"));
        Calendar now = Calendar.getInstance();//获取时间
        grade.setUpdatedate(now.getTime());//当前时间


        return teacherService.updateTeacher(grade);
    }
    @RequestMapping("/itsclassTeacherName")
    @ResponseBody
    public List<Yuangong> itsclassTeacherName(){
        return teacherService.itsclassTeacherName();
    }
    @RequestMapping("/itsjiangshi")
    @ResponseBody
    public List<Jiangshi> itsjiangshi(){
        return teacherService.itsjiangshi();
    }
    @ResponseBody
    @RequestMapping("/gradeAddStu")
    public Map<String,Object> gradeAddStu(@RequestBody String gradeAdd){
        JSONObject jsonObject= JSONObject.parseObject(gradeAdd);
        JSONArray jsonArray=jsonObject.getJSONArray("stuIds");
        String gradeId=jsonObject.getString("gradeId");
        String gradeName=jsonObject.getString("gradeName");
        Integer[] ids=new Integer[jsonArray.size()];
        for(int i=0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            ids[i]=Integer.parseInt(o.toString());
        }
        Map<String,Object> m=new HashMap<>();
        try {
            Integer count = teacherService.gradeAddStu(ids,gradeId,gradeName);
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "添加成功");
            m.put("data", count);

        } catch (PhoenixException e) {
            e.printStackTrace();
            e.printStackTrace();
            m.put(MyContent.CODE, e.getCode());
            m.put(MyContent.MSG, e.getMsg());
            m.put("data", 0);
        }

        return m;
    }
    @RequestMapping("/examHtml")
    public String examHtml(){
        return "exams";
    }
    @RequestMapping("/findExam")
    @ResponseBody
    public List<exam> findExam(){

        List<exam> list = teacherService.findExam();
        System.out.println(list);

        return list;
    }
}
